/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.freshdirect.src.service;
import javax.servlet.http.HttpSession;
import com.freshdirect.src.model.User;
import com.freshdirect.src.dao.UserDAO;
import com.freshdirect.src.model.Order;
import java.util.List;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Dimitris
 */
public class UserService {
    
    
    public String saveUser (User user){
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        UserDAO userDAO = ctx.getBean("userDAO", UserDAO.class);
        
        //Save product to DB
        userDAO.setDataSource("user");
        userDAO.save(user);

        return "User created successfully!";
    }
    
    public String updateUser (User user){
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        UserDAO userDAO = ctx.getBean("userDAO", UserDAO.class);
        
        //Save product to DB
        userDAO.setDataSource("user");
        userDAO.update(user);

        return "User created successfully!";
    }
    public String login (User user){
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        UserDAO userDAO = ctx.getBean("userDAO", UserDAO.class);
        
        //Save product to DB
        userDAO.setDataSource("user");
        userDAO.login(user);

        return "User login successfully!";
    }

    public User ViewUserdetais(String log){
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        UserDAO userDAO = ctx.getBean("userDAO", UserDAO.class);
        
        userDAO.setDataSource("user");
        return userDAO.userdetails(log);
    }

    public List<User> getUsers() {
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        UserDAO userDAO = ctx.getBean("userDAO", UserDAO.class);
        userDAO.setDataSource("admin");
        return userDAO.getAll();
    }

    public List<Order> getAllOrders(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void updateCreditCard(int id,String cardnumber, String holdername, int cardtype) {
        //Get the Spring Context
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        //Get the ProductDAO Bean
        UserDAO userDAO = ctx.getBean("userDAO", UserDAO.class);
        userDAO.setDataSource("admin");
        userDAO.updateCreditCard(id, cardnumber, holdername, cardtype);
    }
    
}
